<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <!--      <el-form-item label="类型" prop="businessType">
              <el-input v-model="dataForm.businessType" placeholder="类型：0充值，1订单"></el-input>
            </el-form-item>
            <el-form-item label="业务id" prop="businessId">
              <el-input v-model="dataForm.businessId" placeholder="业务id"></el-input>
            </el-form-item>
            <el-form-item label="发票id" prop="invoiceId">
              <el-input v-model="dataForm.invoiceId" placeholder="发票id"></el-input>
            </el-form-item>
            <el-form-item label="类型：0普票（电子），1专票（电子）" prop="invoiceType">
              <el-input v-model="dataForm.invoiceType" placeholder="类型：0普票（电子），1专票（电子）"></el-input>
            </el-form-item>
            <el-form-item label="总金额" prop="amount">
              <el-input v-model="dataForm.amount" placeholder="总金额"></el-input>
            </el-form-item>-->
      <el-form-item label="上传电子发票" prop="url" :rules="{ required: '必传'}">
        <el-upload
          :action="dataForm.upload"
          :show-file-list="false"
          :before-upload="beforeUploadHandle"
          :on-success="successHandle">
          <el-icon v-if="dataForm.url" size="30">
            <Ticket/>
          </el-icon>
          <el-icon v-else :size="30">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.invoiceStatus">
          <el-radio :label="0">未开</el-radio>
          <el-radio :label="1">已开</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="状态：充值时：0未支付，1支付成功，可开票。订单时：0申请，1通过" prop="status">
              <el-input v-model="dataForm.status"
                        placeholder="状态：充值时：0未支付，1支付成功，可开票。订单时：0申请，1通过"></el-input>
            </el-form-item>
            <el-form-item label="用户名称" prop="username">
              <el-input v-model="dataForm.username" placeholder="用户名称"></el-input>
            </el-form-item>
            <el-form-item label="用户id" prop="userId">
              <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="note">
              <el-input v-model="dataForm.note" placeholder="备注"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
            </el-form-item>-->
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {getToken} from "@/utils/cache";
import {IObject} from "@/types/interface";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  // businessType: '',
  // businessId: '',
  // invoiceId: '',
  // invoiceType: '',
  // amount: '',
  url: "",
  invoiceStatus: 0,
  // username: '',
  // userId: '',
  // note: '',
  // createTime: '',
  // updateTime: ''
  upload: ""
});

const rules = ref({
  // businessType: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // businessId: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // invoiceId: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // invoiceType: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // amount: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  url: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  status: [{required: true, message: "必填项不能为空", trigger: "blur"}]
  // username: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // userId: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // note: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // createTime: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // updateTime: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.upload = `${window.SITE_CONFIG['apiURLUpload']}?token=${getToken()}&type=invoice`

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/invoicelogs/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 上传之前
const beforeUploadHandle = (file: IObject) => {
  // if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
  //   ElMessage.error("只支持jpg、png、gif格式文件！");
  //   return false;
  // }
};

// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  dataForm.url = res.data.url;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/invoicelogs", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
